package com.longway.core.toolbox.dd.api;
/*package com.longway.core.toolbox.dd.api;

import java.util.concurrent.Callable;
import com.longway.core.toolbox.cache.ICache;
import com.longway.core.toolbox.utils.RetryUtils;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.jfinal.kit.StrKit;

public class DingAccessTokenApi {
    private static final String cachePrefix = "ding:access:";

    *//**
     * 从缓存中获取AccessToken,AccessToken有效期为7200秒,有效期内重复获取返回相同结果,并自动续期。
     * @return AccessToken accessToken
     *//*
    public static DingAccessToken getAccessToken() {
    	ApiConfig config = ApiConfigKit.getApiConfig();
    	DingAccessToken result= getAvailableAccessToken(config);
        return result;
    }
    
    private static DingAccessToken getAvailableAccessToken(ApiConfig config) {
        ICache cache = ApiConfigKit.getCache();
        String accessTokenJson = cache.get(cachePrefix + config.getAppkey());
        DingAccessToken result = null;
        if (StrKit.notBlank(accessTokenJson)) {
        	result = new DingAccessToken(accessTokenJson);
            if (result != null && result.isAvailable()) {
                return result;
            }
        }else{
        	return refreshComponentAccessToken(config);
        }
        return result;
    }

    *//**
     * 直接获取 componentAccessToken 字符串，方便使用
     * @return String componentAccessToken
     *//*
    public static String getAccessTokenStr() {
    	DingAccessToken cat = getAccessToken();
    	if(cat!=null && cat.isAvailable()){
    		return cat.getAccessToken();
    	}else{
        	throw new RuntimeException(cat.getErrorMsg());
        }
    }

    *//**
     * 无条件强制更新AccessToken 值,不再对 cache 中的 token 进行判断
     * @param ApiConfig
     * @return AccessToken
     *//*
    private static DingAccessToken refreshComponentAccessToken(ApiConfig config) {
        String appkey = config.getAppkey();
        String appSecret = config.getAppSecret();
        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(appkey);
        request.setAppsecret(appSecret);
        request.setHttpMethod("GET");
        DingAccessToken result = null;
        try {
			    result = RetryUtils.retryOnException(3, new Callable<DingAccessToken>() {
	            @Override
	            public DingAccessToken call() throws Exception {
	            	OapiGettokenResponse response = client.execute(request);
	    			String json = response.getBody();
	                return new DingAccessToken(json);
	            }
	        });
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        if (null != result && result.isAvailable()) {
            ICache cache = ApiConfigKit.getCache();
            cache.setToken(cachePrefix + config.getAppkey(), result.getCacheJson());
        }
        return result;
    }

}
*/